package com.zyf.cloud.services.userservice.service.impl;

import com.zyf.cloud.common.exception.MyException;
import com.zyf.cloud.services.userservice.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author Administrator
 *  for 写入 库时， 事务分开测试类
 */
@Service
public class BatchInsertTest {


    @Autowired
    private UserServiceImpl userService;

    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
    public void testInsert(int i) {
        testInsert1(i);
        testInsert2(i);
    }

    public void testInsert1(int i) {
        System.out.println("插入" + i);
        UserEntity entity = new UserEntity();
       entity.setId(i);
        entity.setName("zyf" + i);
        entity.setPhoneNumber("1111111111" + i);
        entity.setStatus(1);
        entity.setCreateTime(new Date());
        entity.setLastLoginTime(new Date());
        entity.setLastUpdateTime(new Date());
        userService.save(entity);
        if (i == 106) {
            throw new MyException("测试");
        }
    }
    public void testInsert2(int i) {
        System.out.println("插入" + i);
        UserEntity entity = new UserEntity();
        entity.setId(i+50);
        entity.setName("zyf2" + i);
        entity.setPhoneNumber("11111111112" + i);
        entity.setStatus(1);
        entity.setCreateTime(new Date());
        entity.setLastLoginTime(new Date());
        entity.setLastUpdateTime(new Date());
        userService.save(entity);
        if (i == 106) {
            throw new MyException("测试");
        }
    }
}
